package com.wbao.dianniu.ui;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.R;
import com.wbao.dianniu.customView.CustomDialog;
import com.wbao.dianniu.listener.IActivityDelListener;
import com.wbao.dianniu.logical.Const;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.ActivityDelManager;
import com.wbao.dianniu.update.ReleaseDataManager;
import com.wbao.dianniu.update.ShareSDKManager;
import com.wbao.dianniu.utils.Notification;

/**
 * 活动管理
 *
 * @author yejianjun
 */
public class ActManagerActivity extends BaseActivity implements IActivityDelListener {

    private Button rightBtn;
    private TextView tv_browse;
    private TextView tv_signup;
    private TextView tv_pass;
    private Button btn_registration;
    private Button btn_modify;
    private Button btn_delete;
    private Button btn_share;
    private int id;
    private String address;
    private String price;
    private String subject;
    private String maxPersion;
    private String startDate;
    //	private String content;
    private ActivityDelManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_act_manager, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        setTitleName(getResources().getString(R.string.act_manager));
        init();
        initData();
        manager = new ActivityDelManager(this);
        manager.addActivityDelListener(this);

    }

    private void init() {
        rightBtn = getRightTextButton();
        rightBtn.setText("查看详情");
        rightBtn.setOnClickListener(this);

        tv_browse = findViewById(R.id.browse_tv);
        tv_signup = findViewById(R.id.sign_up_tv);
        tv_pass = findViewById(R.id.pass_tv);
        btn_registration = findViewById(R.id.registration_btn);
        btn_modify = findViewById(R.id.modify_act_btn);
        btn_delete = findViewById(R.id.delete_act_btn);
        btn_share = findViewById(R.id.share_act_btn);
        btn_registration.setOnClickListener(this);
        btn_modify.setOnClickListener(this);
        btn_delete.setOnClickListener(this);
        btn_share.setOnClickListener(this);

    }

    private void initData() {
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra(Const.BUNDLE_DATA);
        if (null != bundle) {
            tv_browse.setText(bundle.getString(Const.BUNDLE_BROWSE, ""));
            tv_signup.setText(bundle.getString(Const.BUNDLE_SIGNUP, ""));
            tv_pass.setText(bundle.getString(Const.BUNDLE_PASS, ""));
            id = bundle.getInt(Const.BUNDLE_ID, 0);
            address = bundle.getString(Const.BUNDLE_ADDRESS, "");
            price = bundle.getString(Const.BUNDLE_PRICE, "");
            subject = bundle.getString(Const.BUNDLE_SUBJECT, "");
            maxPersion = bundle.getString(Const.BUNDLE_MAXPERSION, "");
            startDate = bundle.getString(Const.BUNDLE_STARTDATE, "");
//			content = bundle.getString(Const.BUNDLE_CONTENT, "");
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.registration_btn:
                Intent intent = new Intent();
                intent.putExtra(Const.BUNDLE_ID, id);
                intent.setClass(this, SignUpManagerActivity.class);
                startActivity(intent);
                break;
            case R.id.modify_act_btn:
                Intent modifyIntent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putInt(Const.BUNDLE_ID, id);
                bundle.putString(Const.BUNDLE_ADDRESS, address);
                bundle.putString(Const.BUNDLE_PRICE, price);
                bundle.putString(Const.BUNDLE_SUBJECT, subject);
                bundle.putString(Const.BUNDLE_MAXPERSION, maxPersion + "人");
                bundle.putString(Const.BUNDLE_STARTDATE, startDate);
//			bundle.putString(Const.BUNDLE_CONTENT, content);
                modifyIntent.putExtra(Const.BUNDLE_EDIT, bundle);
                modifyIntent.setClass(this, CreateActivity.class);
                startActivity(modifyIntent);
                break;
            case R.id.delete_act_btn:
                CustomDialog.Builder builder = new CustomDialog.Builder(this);
                builder.setTitle("确认删除该条活动吗？");
                builder.setPositiveButton(getResources().getString(R.string.confirm),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                manager.activityDel(GlobalContext.getAccountId(), id);
                            }
                        });
                builder.setNegativeButton(getResources().getString(R.string.cancel),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                builder.create().show();
                break;
            case R.id.share_act_btn:
                // 调用分享内容
                String imageUrl = GlobalContext.cdndownUrl + "logo.png";
                String content = "活动主题：" + subject + "\n" + "活动时间：" + startDate + "\n" + "活动地点：" + address;
                ShareSDKManager shareSDKManager = ShareSDKManager.getInstance();
                shareSDKManager.showShare(this, content, 6, imageUrl, id);
                break;
            case R.id.back_button:
                finish();
                break;
            case R.id.right_button:
                Intent rightIntent = new Intent();
                rightIntent.putExtra(Const.INTENT_ACTIVITYID, id);
                rightIntent.setClass(this, ActivityDetail.class);
                startActivity(rightIntent);
                break;
            default:
                break;
        }

    }

    @Override
    public void onActivityDelFailure(int errorCode, String errorMsg) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onActivityDelSuccess() {
        // TODO Auto-generated method stub
        Notification.toast(this, "活动已删除");
        //删除该条记录
        ReleaseDataManager releaseDataManager = ReleaseDataManager.getInstance();
        releaseDataManager.delete(id);
        finish();
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(null != manager){
            manager.removeActivityDelListener(this);
        }
    }
}
